package cn.bugstack.mybatis.binding;

import java.io.Serializable;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.Map;

import cn.bugstack.mybatis.session.SqlSession;

/**
 * description: mapper代理
 * taskId：
 * <pre>
 * change history:
 * date             defect#             person                comments
 * ---------------------------------------------------------------------
 * 2024/8/28       ********         zhaochaoyue        create file
 * </pre>
 *
 * @author: zhaochaoyue
 * @date: 2024/8/28 20:22
 * @copyright: 2024
 */
public class MapperProxy<T> implements InvocationHandler, Serializable {
    
    private static final long serialVersionUID = -6424540398559729838L;
    
    private SqlSession sqlSession;
    
    private final Class<T> mapperInterface;

    public MapperProxy(SqlSession sqlSession, Class<T> mapperInterface) {
        this.sqlSession = sqlSession;
        this.mapperInterface = mapperInterface;
    }

    //public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    //    if(Object.class.equals(method.getDeclaringClass())){
    //        return method.invoke(this,args);
    //    }else{
    //        return "你的被代理了！"+sqlSession.get(mapperInterface.getName()+"."+method.getName());
    //    }
    //    
    //}

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        if (Object.class.equals(method.getDeclaringClass())) {
            return method.invoke(this, args);
        } else {
            return sqlSession.selectOne(method.getName(), args);
        }
    }
}
